<?php

require_once dirname(__FILE__) . '/../lib/doctor_health_adviceGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/doctor_health_adviceGeneratorHelper.class.php';

/**
 * doctor_health_advice actions.
 *
 * @package    docplus.co.za
 * @subpackage doctor_health_advice
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class doctor_health_adviceActions extends autoDoctor_health_adviceActions
{

    public function preExecute()
    {
        if (is_object($this->getUser()->getDoctor()))
        {
            if ($this->getUser()->getType() == "Doctor")
            {
                $this->doctorId = $this->getUser()->getDoctor()->getId();
            }
            else
            {
                $this->doctorId = $this->getUser()->getDoctorId();
            }
        }
        else
        {
            $this->doctorId = "undefined";
        }
        parent::preExecute();
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->doctor = DoctorTable::getInstance()->findOneById($request->getParameter("doctor_id"));
    }

    /**
     * Executes done action
     *
     * @param sfRequest $request A request object
     */
    public function executeShow(sfWebRequest $request)
    {
        $this->doctorHealthAdvice = DoctorHealthAdviceTable::getInstance()->findOneById($request->getParameter("advice_id"));
    }

    /**
     * Executes advice action
     *
     * @param sfRequest $request A request object
     */
    public function executeNew(sfWebRequest $request)
    {
        if ($this->doctorId == "undefined")
            die("signin");

        $doctorHealthAdvice = new DoctorHealthAdvice();
        $doctorHealthAdvice->setDoctorId($this->doctorId);

        $this->form = new DoctorHealthAdviceForm($doctorHealthAdvice);
        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $doctorHealthAdviceId = $this->form->save()->getId();
                    $this->getUser()->setFlash('notice', 'Thanks for successfully submitting your health advice on our system.');
                    echo $doctorHealthAdviceId;
                    exit;
                }
                catch (Exception $e)
                {                    
                }
            }
        }
    }

    /**
     * Executes rate action
     *
     * @param sfRequest $request A request object
     */
    public function executeRate(sfWebRequest $request)
    {
        if ($request->hasParameter("advice_id") && $request->hasParameter("rating_value"))
        {
            DoctorHealthAdviceRatingTable::getInstance()->updateOrCreateByDoctorHealthAdviceIdAndRatingValue($request->getParameter("advice_id"), $request->getParameter("rating_value"));
            $this->doctorHealthAdvice = DoctorHealthAdviceTable::getInstance()->findOneById($request->getParameter("advice_id"));
        }
    }

}
